package offer;

import java.util.Stack;

public class erchashuJX27 {
    class Solution {
        public class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode(int x) { val = x; }
        }
/*        public TreeNode mirrorTree(TreeNode root) {
            if (root==null)return null;
            TreeNode tmp = root.left;
            root.left = mirrorTree(root.right);
            root.right = mirrorTree(tmp);
            return root;
        }*/

        public TreeNode mirrorTree(TreeNode root) {
            if (root==null)return null;
            Stack<TreeNode> stack = new Stack<TreeNode>(){{add(root);}};

            while (!stack.isEmpty()){
                TreeNode node = stack.pop();
                if (node.left!=null) stack.add(node.left);
                if (node.right!=null) stack.add(node.right);
                TreeNode tmp = node.left;
                node.left=node.right;
                node.right=tmp;
            }
            return root;
        }
    }
}
